package world.xuewei.controller;

import org.springframework.web.bind.annotation.*;
import world.xuewei.model.entity.ShopNotice;
import world.xuewei.service.ShopActivityService;
import world.xuewei.model.entity.R;
import world.xuewei.model.entity.ShopActivity;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 商家活动表(shop_activity)表控制层
 *
 * @author makejava
 * @since 2024-11-15 19:27:51
 */

@RestController
@RequestMapping("/activity")
public class ShopActivityController {

    /**
     * 服务对象
     */
    @Resource
    private ShopActivityService shopActivityService;

    // 获取店铺活动（商家端）
    @GetMapping("/getAllActivity")
    public R getAllShopActivity(HttpServletRequest request) {
        String userIdStr = (String) request.getAttribute("userId");
        if (userIdStr == null) {
            return R.error().message("用户未登录 or 携带token请求");
        }
        Long userId = Long.parseLong(userIdStr);
        List<ShopActivity> shopActivityList = shopActivityService.getAllShopActivity(userId);
        return R.ok().data(shopActivityList);
    }

    // 获取店铺活动（用户端）
    @GetMapping
    public R getShopActivity(@RequestParam("merchant_id") Long merchant_id) {
        List<ShopActivity> shopActivity = shopActivityService.getShopActivityList(merchant_id);
        return R.ok().data(shopActivity);
    }

    // 添加商家活动
    @PostMapping
    public R addShopActivity(@RequestBody ShopActivity shopActivity) {
        int i = shopActivityService.addShopActivity(shopActivity);
        if (i > 0) {
            return R.ok().message("添加成功");
        } else {
            return R.error().message("添加失败");
        }
    }

    // 修改商家活动
    @PutMapping
    public R updateShopActivity(@RequestBody ShopActivity shopActivity) {
        int i = shopActivityService.updateShopActivity(shopActivity);
        if (i > 0) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("修改失败");
        }

    }

    // 删除
    @DeleteMapping("{id}")
    public R deleteShopActivity(@PathVariable(value = "id") Long id) {
        int i = shopActivityService.deleteShopActivity(id);
        if (i > 0) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }
}
